<?php
namespace app\admin\controller;

use app\admin\model\System as SystemModel;

class System extends Base {

    public $cModel = '';

    public function _initialize() {
        parent::_initialize();
        $this->cModel = new SystemModel();
    }

    public function index(){
        $data = $this->cModel->find();
        $data['reward'] = unserialize($data['reward']);
        $this->assign('data', $data);
        return $this->fetch();
    }

    public function edit(){
        $data = input('');
//        $data['reward'] = [
//            'j1'=>$data['reward_desc'][0],
//            'j2'=>$data['reward_desc'][1],
//            'j3'=>$data['reward_desc'][2],
//            'j4'=>$data['reward_desc'][3],
//            'content'=>$data['reward_details'],
//        ];
//        $data['reward'] = serialize($data['reward']);
        $r = $this->cModel->allowField(true)->save($data, ['id'=>1]);
        $r ? $msg='操作成功' : $msg='操作失败';
        return $this->ajaxReturn(1, $msg);
    }

    // 刷新签到二维码
    public function ajax_sign_qrcode(){
        $info = $this->cModel->where('id', 1)->value('sign_qrcode');
        return json(['code'=>$info]);
    }

    // 楼盘信息
    public function houses_edit(){
        if(request()->isPost()){
            $params = input('post.');
            $params['imgs'] = implode(',', $params['imgs']);

            foreach($params['house_type_title'] as $k=>$v){
                if($v){
                    $params['house_type'][$k] = [$params['house_type_title'][$k], $params['house_type_img'][$k]];
                }
            }
            $params['house_type'] = serialize($params['house_type']);
            unset($params['file'], $params['house_type_title'], $params['house_type_img']);
            $res = db('houses')->where('id',1)->update($params);
            $res ? $msg = '操作成功' : $msg = '操作失败';
            return $this->ajaxReturn(1, $msg);
        }else{
            $data = db('houses')->where('id',1)->find();
            $data['imgs'] = explode(',', $data['imgs']);
            $data['house_type'] = unserialize($data['house_type']);
            $this->assign('data', $data);
            return $this->fetch();
        }
    }

    // 奖励信息
    public function reward_edit(){
        if(request()->isPost()){
            $params = input('post.');
            $ranking_content = [];
            foreach($params['ranking_title'] as $k=>$v){
                if(!empty($v)){
                    $ranking_content[$k][] = $params['ranking_title'][$k];
                    $ranking_content[$k][] = $params['ranking_des'][$k];
                }
            }
            unset($params['ranking_title'], $params['ranking_des']);
            $params['ranking_content'] = serialize($ranking_content);
            $res = db('reward')->where('id',1)->update($params);
            $res ? $msg = '操作成功' : $msg = '操作失败';
            return $this->ajaxReturn(1, $msg);
        }else{
            $data = db('reward')->where('id',1)->find();
            $data['ranking_content'] = unserialize($data['ranking_content']);
            $this->assign('data', $data);
            return $this->fetch();
        }
    }

}